package com.caidanxiaoyuan.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import com.caidanxiaoyuan.model.DiZhiList;
import com.caidanxiaoyuan.model.HuoDongList;
import com.caidanxiaoyuan.model.HuoDongLiuYanListView;
import com.caidanxiaoyuan.model.ShengHuoDingDanListView;
import com.caidanxiaoyuan.model.UserList;
import com.caidanxiaoyuan.service.DiZhiListService;
import com.caidanxiaoyuan.service.HuoDongListService;
import com.caidanxiaoyuan.service.ShengHuoDingDanListViewService;
import com.caidanxiaoyuan.service.UserListService;
import com.caidanxiaoyuan.util.JsonUtil;
import com.caidanxiaoyuan.util.MD5Util;
import com.caidanxiaoyuan.util.ResponseUtil;
import com.caidanxiaoyuan.util.SMSUtil;
import com.github.pagehelper.Page;

@Controller
@CrossOrigin
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserListService userListService;
	@Autowired
	private DiZhiListService diZhiListService;
	@Autowired
	private ShengHuoDingDanListViewService shengHuoDingDanListViewService;
	@Autowired
	private HuoDongListService huoDongListService;
	
	/**
	 * 
	* @Description: 短信发送  
	* @author zealot
	* @param @param e
	* @param @param response
	* @param @throws Exception 
	* @return void    
	* @version 创建时间：2017年2月28日 上午9:46:56  
	* @throws
	 */
	@RequestMapping("/sendsms")
	public void sendsms(UserList e,HttpServletResponse response) throws Exception{
		List<UserList> u =  userListService.selectByUs(e);
		UserList u1 = null;
		if(u!=null){
			if(u.size()>0)
			u1 = u.get(0);
		} 
		String ram = getRam();
		switch(e.getAction())
	    {
	        case "reg":
	        	 if(u1!=null)
	             {
	        		 ResponseUtil.write(response, 15);
	             }
	            SMSUtil.batchSend(e.getTelphone().toString(), "您的验证码为："+ram+"，请尽快完成验证。如非本人操作，请忽略短信。");
	            break;
	        case "pwd":
	        	 if(u1!=null)
	             {
	        		 ResponseUtil.write(response, 16);
	             }
	            SMSUtil.batchSend(e.getTelphone().toString(), "您的验证码为："+ram+"，请尽快完成验证。如非本人操作，请忽略短信。");
	            break;
	        default :
	        	SMSUtil.batchSend(e.getTelphone().toString(), "验证码："+ram);
	            break;
	    }
		ResponseUtil.write(response, ram);
	}
	
	/**
	 * 
	* @Description: 修改密码  
	* @author zealot
	* @param @param e
	* @param @param response
	* @param @throws Exception 
	* @return void    
	* @version 创建时间：2017年2月28日 上午9:47:09  
	* @throws
	 */
	@RequestMapping("/resetpwd")
	public void resetpwd(UserList e,HttpServletResponse response) throws Exception{
		e.setPwd(MD5Util.getMD5(e.getPwd()));
		userListService.update(e);
		ResponseUtil.write(response, 7);
	}
	
	/**
	 * 
	* @Description: 彩中心  
	* @author zealot
	* @param @param e
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年2月28日 上午9:47:19  
	* @throws
	 */
	@RequestMapping("/view")
	public void view(UserList e,HttpServletResponse response){
		UserList ne = userListService.selectByPrimaryKey(e.getMyid());
		DiZhiList dz = new DiZhiList();
		dz.setMyid(ne.getMyid());
		List<DiZhiList> d = diZhiListService.selectByUs(dz);
		if(d!=null){
			if(d.size()>0){
				dz = d.get(0);
				ne.setDizhiid(dz.getId().toString());
				ne.setShengid(dz.getShengid().toString());
				ne.setShiid(dz.getShiid().toString());
				ne.setXianid(dz.getXianid().toString());
				ne.setDizhi(dz.getDizhi());
			}
		}
		ResponseUtil.result(response, JsonUtil.toJsonStr(ne));
	}
	
	
	/**
	 * @throws Exception 
	 * 
	* @Description: 地址添加  
	* @author zealot
	* @param @param e
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年2月28日 上午9:47:33  
	* @throws
	 */
	@RequestMapping("/dizhi")
	public void dizhi(DiZhiList e,HttpServletResponse response) throws Exception{
		List<DiZhiList> n = diZhiListService.selectByUs(e);
		if(n!=null){
			if(n.size()>0){
				diZhiListService.update(e);
				ResponseUtil.write(response, "{}");
			}else{
				diZhiListService.insert(e);
				Map p = new HashMap();
				p.put("dizhiid", e.getId());
				ResponseUtil.result(response, JsonUtil.toJsonStr(p));
			}
		}
	}
	
	@RequestMapping("/dingdan")
	public void dingdan(Page<ShengHuoDingDanListView> page,HttpServletResponse response,ShengHuoDingDanListView t){
		ResponseUtil.result(response, JsonUtil.toJsonStr(shengHuoDingDanListViewService.selectAll(page, t)));
	}
	
	/**
	 * 
	* @Description: 注册  
	* @author zealot
	* @param @param e
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年2月28日 上午9:47:42  
	* @throws
	 */
	@RequestMapping("/reg")
	public void reg(UserList e,HttpServletResponse response){
		try{
			if(e.getRegcode().equals(e.getSmscode()))
		    {
		        ResponseUtil.write(response, 3);
		    }
		    if(Pattern.compile("/^.{1,10}$/u").matcher(e.getNickname()).matches())
		    {
		        ResponseUtil.write(response, 5);
		    }
		    if(Pattern.compile("^[1]([3][0-9]{1}|59|58|88|89)[0-9]{8}$").matcher(e.getAccount()).matches())
		    {
		        ResponseUtil.write(response, 6);
		    }
		    if(Pattern.compile("/^.{6,}$/").matcher(e.getPwd()).matches())
		    {
		        ResponseUtil.write(response, 7);
		    }
		    List<UserList> u =  userListService.selectByUs(e);
			UserList u1 = null;
			if(u!=null){
				if(u.size()>0)
				ResponseUtil.write(response, 4);
			}
			e.setShijian((int) Long.parseLong(String.valueOf(System.currentTimeMillis()).toString().substring(0,10)));
			e.setJifen(0);
			e.setPwd(MD5Util.getMD5(e.getPwd()));
			userListService.insert(e);
		}catch (Exception ex) {
			Map p = new HashMap();
			p.put("infocode", 8);
			ResponseUtil.result(response, JsonUtil.toJsonStr(p));
		}
	}
	
	/**
	 * 
	* @Description: 登录  
	* @author zealot
	* @param @param e
	* @param @param response
	* @param @throws Exception 
	* @return void    
	* @version 创建时间：2017年3月2日 上午9:17:14  
	* @throws
	 */
	@RequestMapping("/login")
	public void login(UserList e,HttpServletResponse response) throws Exception{
		UserList ne = userListService.selectByAccount(e);
		if(ne==null){
			Map p = new HashMap();
			p.put("infocode", 1);
			ResponseUtil.write(response, p);
		}
		if(!ne.getPwd().equals(MD5Util.getMD5(e.getPwd()))){
			Map p = new HashMap();
			p.put("infocode", 2);
			ResponseUtil.write(response, p);
		}
		ne.setUsertoken(MD5Util.getMD5(ne.getMyid()+ne.getPwd()));
		
		DiZhiList dz = new DiZhiList();
		dz.setMyid(ne.getMyid());
		List<DiZhiList> d = diZhiListService.selectByUs(dz);
		if(d!=null){
			if(d.size()>0){
				dz = d.get(0);
				ne.setDizhiid(dz.getId().toString());
				ne.setShengid(dz.getShengid().toString());
				ne.setShiid(dz.getShiid().toString());
				ne.setXianid(dz.getXianid().toString());
				ne.setDizhi(dz.getDizhi());
			}
		}
		ResponseUtil.result(response, JsonUtil.toJsonStr(ne));
	}
	
	/**
	 * 
	* @Description: 我的活动
	* @author zealot
	* @param @param page
	* @param @param e
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年3月2日 上午9:21:40  
	* @throws
	 */
	@RequestMapping("/huodong")
	public void huodong(Page<HuoDongList> page,HuoDongList e,HttpServletResponse response){
		Map<String, Object> map = new HashMap<String, Object>();
		Page<HuoDongList> list = (Page<HuoDongList>)huoDongListService.selectAll(page, e);
		map.put("data", list.getResult());
		map.put("totalpage", list.getPages());
		map.put("totalrows", list.getTotal());
		map.put("time", String.valueOf(System.currentTimeMillis()).toString().substring(0,10));
		ResponseUtil.result(response, JsonUtil.toJsonStr(map));
	}
	
	/**
	 * @throws Exception 
	 * 
	* @Description: 用户编辑  
	* @author zealot
	* @param @param e
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年3月2日 上午9:45:49  
	* @throws
	 */
	@RequestMapping("/edit")
	public void edit(UserList e,HttpServletResponse response) throws Exception{
		userListService.update(e);
		ResponseUtil.write(response, 12);
	}
	
	
	public String getRam(){
		int r1 = (int)(1+Math.random()*(9-1+1));
		int r2 = (int)(1+Math.random()*(9-1+1));
		int r3 = (int)(1+Math.random()*(9-1+1));
		int r4 = (int)(1+Math.random()*(9-1+1));
		return String.valueOf(r1)+String.valueOf(r2)+String.valueOf(r3)+String.valueOf(r4);
	}
	
	public static void main(String[] args) {
		System.out.println(MD5Util.getMD5("111111"));
	}
}
